package org.muninn.niagara.services;

import java.io.PrintWriter;

import javax.baja.control.BControlPoint;
import javax.baja.driver.BDeviceFolder;
import javax.baja.naming.BOrd;
import javax.baja.sys.Action;
import javax.baja.sys.BComponent;
import javax.baja.sys.BIcon;
import javax.baja.sys.Flags;
import javax.baja.sys.Property;
import javax.baja.sys.Sys;
import javax.baja.sys.Type;

public class BDeviceInfoService extends BMuninnService
{
  /** Path to the file containing the exported info */
  public static final Property filePath = newProperty(0, "C:\\Documents and Settings\\Administrator\\Desktop\\DeviceInfo.txt\\");
  public void setFilePath(String v) { setString(filePath, v); }
  public String getFilePath() { return getString(filePath); }
  
  public static final Action execute = newAction(Flags.ASYNC | Flags.DEFAULT_ON_CLONE, null);
  public void execute() { invoke(execute, null, null); }
  public void doExecute() throws Exception { export(); }
  
  private static BDeviceInfoService INSTANCE = null;
  
  private PrintWriter writer;
  private boolean inCont = false;
  
  public void export() throws Exception
  {
    System.out.println("Starting device info export... ");
    writer = new PrintWriter(getFilePath(), "UTF-8");
    
    BComponent comp = BOrd.make("station:|slot:/Drivers").resolve().getComponent();
    process(comp);
    
    writer.close();
    System.out.println("Export finished.");
  }
  
  private void process(BComponent comp)
  {
    comp.lease();
    
    //case: device folder found (begin section, recurse)
    if (comp instanceof BDeviceFolder)
    {
      BComponent[] devs = comp.getChildComponents();
      
      for (int i = 0; i < devs.length; i++)
      {
        inCont = true;
        System.out.print("Exporting info for " + devs[i].getName() + "... ");
        
        writer.println(devs[i].getName() + ":");
        process(devs[i]);
        writer.println();
        
        System.out.println("Done");
        inCont = false;
      }
    }
    
    //case: point found (base case, record info)
    else if (comp instanceof BControlPoint)
    {
      if (inCont)
      {
        String type = comp.getType().toString();
        writer.println("  " + comp.getName() + " (" + type.substring(type.lastIndexOf(":") + 1) + ")");
      }
      
      else
        System.out.println("Ignoring invalid point (" + comp.getName() + ")");
    }
    
    //case: other object found (recurse)
    else
    {
      BComponent[] childs = comp.getChildComponents();
      for (int i = 0; i < childs.length; i++)
        process(childs[i]);
    }
  }
  
  public BIcon getIcon() { return icon; }
  private static final BIcon icon = BIcon.make("local:|module://muninn/org/muninn/niagara/graphics/MuninnGroup.PNG");
  
  public static final Type TYPE = Sys.loadType(BDeviceInfoService.class);
  public Type[] getServiceTypes() { return new Type[] { TYPE }; }
  public Type getType() { return TYPE; }
  
  public final synchronized void serviceStarted() throws Exception { INSTANCE = this; }
  
  public final synchronized void serviceStopped() throws Exception { INSTANCE = null; }
  
  public static final BDeviceInfoService getInstance() { return INSTANCE; }
}
